/**
 * 
 */
package com.hwadee.JPKC.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwadee.JPKC.entity.user;
import com.hwadee.JPKC.service.ConmmentService;
import com.hwadee.JPKC.service.UserService;

/**
 * 
 * @ClassName: Test
 * 
 * @Description: TODO(这里用一句话描述这个类的作用)
 * 
 * @author hui
 * 
 * @date 2020-03-03
 * 
 * @Copyright ? 四川华迪信息有限公司
 * 
 */

@Controller
@RequestMapping("/admin/User")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private ConmmentService conmmentService;

	@RequestMapping("/userInfo")
	public String fontawesome(@RequestParam(name = "p", required = true, defaultValue = "1") Integer p, Model model) {
		Page<user> users = PageHelper.startPage(p, 5).doSelectPage(() -> userService.getAll());
		model.addAttribute("users", users);
		return "admin/userInfo";
	}

	@PostMapping("/findPM")
	public @ResponseBody List<user> findpermission(
			@RequestParam(name = "p", required = true, defaultValue = "1") Integer p, String userRole, Model model) {

		Page<user> users = PageHelper.startPage(p, 5).doSelectPage(() -> userService.findPm(userRole));

		model.addAttribute("users", users);
		return users;
	}

	@ModelAttribute("permission")
	public Map<String, String> getpermission() {
		Map<String, String> permission = new HashMap<String, String>();
		permission.put("老师", "老师");
		permission.put("专家", "专家");
		return permission;
	}

	@RequestMapping("/delete")
	public String Delete(@ModelAttribute("uId") int uId) {
		conmmentService.delete(uId);
		userService.delete(uId);
		return "redirect:/admin/User/userInfo";
	}

	/*
	 * 用户登录页
	 */
	@RequestMapping("/teacherLogin")
	public String teacherLogin() {
		return "teacherLogin";
	}

	/*
	 * 用户登陆控制 注意角色处理问题
	 */
	@RequestMapping("/examineLosin")
	public String teacherLogin(user user, Model model, HttpSession session) {
		String userName = user.getUserName();
		String userPw = user.getUserPw();
		user u = userService.examineUserName(userName);
		String userRole = user.getUserRole();
		if (u.getUserName().equals(userName) && u.getUserPw().equals(userPw) && u.getUserRole().equals(userRole)) {

			model.addAttribute("u", u);
			session.setAttribute("u", u);
			return "redirect:/index/";
		} else {
			return "teacherLogin";
		}

	}

	/*
	 * 老师个人信息页
	 */
	@RequestMapping("/gr.do")
	public String gr() {
		return "gr";
	}

	/*
	 * 老师个人信息更新
	 */
	@RequestMapping("/xiugaigr")
	public String xiugaigr(user user, Integer userId, HttpServletRequest requset, HttpSession session, Model model) {
		if (user.getUserPw() != null) {
			userService.updateByPrimaryKeySelective(user);
			session.setAttribute("msg", "修改成功!!");
			user u = userService.selectUserId(userId);
			session.setAttribute("u", u);
			String url = "redirect:/admin/User/gr?userId=" + userId;
			return url;
		} else {
			session.setAttribute("msg", "请填写6-16位密码!!");
			String url = "redirect:/admin/User/gr?userId=" + userId;
			return url;
		}
	}

	/*
	 * 用户注册页
	 */
	@RequestMapping("/zhuceshow")
	public String zhuceshow() {
		return "zc";
	}

	/*
	 * 用户注册控制
	 */
	@PostMapping("/zhuce")
	public String zhuce(user user) {
		userService.insert(user);
		return "redirect:/admin/User/teacherLogin";
	}

	/*
	 * 用户注销
	 */
	@RequestMapping("/zhuxiao")
	public String zhuxiao(user user, HttpSession session) {
		session.removeAttribute("u");
		return "redirect:/index/";

	}

}
